#include <unistd.h>
#include <ti/drivers/GPIO.h>
#include <pthread.h>

#include "DS18B20/DS18B20.h"
#include "ti_drivers_config.h"
#include "dbger.h"

#define NUM_DS      7

static float temp[NUM_DS] = {0};
const uint8_t numberNo[NUM_DS] = {3, 1, 0, 5, 6, 2, 4};

/* Thread function prototypes */
extern "C" {
    #if defined(DBG_DISPLAY)
    extern void APP_openDisplay(void);
    #endif
    void  *mainThread(void *arg0);
}

/*
 *  ======== mainThread ========
 */
void *mainThread(void *arg0)
{
    DBG_INIT();
    DBG_INFO("/******** DS18B20 Project Start ********/");

    GPIO_setConfig(CONFIG_GPIO_LED_0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);
    GPIO_setConfig(DS18B20_PIN, GPIO_CFG_OUT_OD_NOPULL | GPIO_CFG_OUT_HIGH);
    GPIO_write(DS18B20_PIN, 1);

    DS18B20 ds;

//    while(1) {
//        DBG_INFO("PIN read: %d", GPIO_read(DS18B20_PIN));
//        sleep(1);
//    }

    DBG_INFO("No.1,No.2,No.3,No.4,No.5,No.6,No.7");

    int i;
    while(1) {
        sleep(1);
        //GPIO_toggle(CONFIG_GPIO_LED_0);
        i = NUM_DS;
        while(i) {
            i--;
            temp[i] = 0;
        }
        while(ds.selectNext()) {
            temp[numberNo[i]] = ds.getTempC();
            //DBG_INFO("Current Temp: temp[%d] = %f", numberNo[i], temp[numberNo[i]]);
            i++;
        }
        DBG_INFO("%f,%f,%f,%f,%f,%f,%f", temp[0],temp[1],temp[2],temp[3],temp[4],temp[5],temp[6]);
    }
}
